
module.exports = plop => {
  //参数首字母小写
  plop.setHelper("firstLower", function (text) {
    return text.replace(text[0], text[0].toLowerCase());
  });
  //字母转小写
  plop.setHelper("lower", function (text) {
    return text.toLowerCase().replace(/-/g, "");
  });
  //参数首字母大写
  plop.setHelper("upper", function (text) {
    return text.replace(text[0], text[0].toUpperCase());
  });
  //把-变成大写
  plop.setHelper("handle", function (text) {
    const arr = text.split("-");
    for (let i = 0; i < arr.length; i++) {
      arr[i] = arr[i].replace(arr[i][0], arr[i][0].toUpperCase());
    }
    return arr.join("");
  });
  //首字母大写并且把-变成大写
  plop.setHelper("upperAnd_", function (text) {
    const upperText = text.replace(text[0], text[0].toUpperCase());
    const arr = upperText.split("-");
    for (let i = 0; i < arr.length; i++) {
      arr[i] = arr[i].replace(arr[i][0], arr[i][0].toUpperCase());
    }
    return arr.join("");
  });

  plop.setWelcomeMessage("请选择需要创建的模式：");
  //生成路由菜单
  plop.setGenerator("generateView", {
    description: "生成路由菜单",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "请输入菜单名称(仅支持英文小写和连接符'-'):"
      },
      {
        type: "confirm",
        name: "isFirstLevel",
        message: "是否生成一级菜单？"
      }
    ],
    actions: data => {
      //无论是全局菜单还是一级菜单，均在views下新增vue文件
      const res = [
        //新增vue文件
        {
          type: "add",
          path: "src/views/{{name}}/index.vue",
          templateFile: "src/template/index.vue.hbs"
        },
        //新增测试模块
        {
          type: "add",
          path: "src/views/{{name}}/__test__/{{name}}.spec.ts",
          templateFile: "src/template/__test__.ts.hbs"
        }
      ];

      if (data.isFirstLevel) {
        //1级菜单增加新的router模块ts
        res.push(
          //新增router模块文件
          {
            type: "add",
            path: "src/router/modules/{{name}}.ts",
            templateFile: "src/template/router.modules.ts.hbs"
          }

          //菜单排序 排序配置在demo中被干掉了，因此屏蔽排序生成部分
          // {
          //   type: "append",
          //   path: "src/router/enums.ts",
          //   pattern: /\/\/ -- APPEND RANK HERE --/gi,
          //   template: " {{lower name}} = 100,"
          // },
          // {
          //   type: "append",
          //   path: "src/router/enums.ts",
          //   pattern: /\/\/ -- APPEND EXPORT RANK HERE --/gi,
          //   template: "{{lower name}},"
          // }
        );
      } else {
        //增加到remaining.ts中
        res.push({
          type: "append",
          path: "src/router/modules/remaining.ts",
          pattern: /\/\/ -- APPEND ROUTER ITEM --/gi,
          template: `{
              path: "/{{name}}",
              name: "{{upperAnd_ name}}",
              component: () => import("@/views/{{name}}/index.vue"),
              meta: {
                title: "{{upperAnd_ name}}",
                showLink: false,
                rank:200
              }
            },`
        });
      }
      return res;
    }
  });

  /** 子菜单实现方案（plan):
   * 1. 解析url获取${module}.js
   * 2. 定义一个字符串模版，把{ } 前后的东西都定义出来
   * 3. 使用正则匹配，把中间部分匹配出来 [hard]
   * 4. 匹配出来的字符串转为json对象
   * 5. 解析url结构，找到需要增加的那一层的children
   * 6. 定义单体路由对象，插入新路由
   * 7. 转成字符串，填充进2定义的字符串模版里面里面
   * 8. 写入文件
   * ps: 有同步获取ts文件暴露内容的方法比较好办
   */

  //仅生成vue组件
  plop.setGenerator("generateComponent", {
    description: "仅生成vue组件",
    prompts: [
      {
        type: "input",
        name: "url",
        message: "请输入组件路径(以src/views为一级路径,若生成至views下,则为空):"
      },
      {
        type: "input",
        name: "name",
        message: "请输入组件名称(仅支持英文小写和连接符'-'):"
      }
    ],
    actions: [
      //新增vue文件
      {
        type: "add",
        path: "src/views/{{url}}/{{name}}.vue",
        templateFile: "src/template/index.vue.hbs"
      }
    ]
  });
};
